<template>
  <div class="login">
    <div class="loginBox">
      <div class="title">
        <img class="img" src="@/assets/img/login_logo.png" alt="" />
        <h3>黑马面面 | 用户登录</h3>
      </div>
      <div class="form">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item class="one" prop="phone">
            <el-input
              v-model="form.phone"
              prefix-icon="el-icon-user"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <el-form-item class="two" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item class="three" prop="code">
            <div class="input">
              <el-input
                v-model="form.code"
                class="codeText"
                prefix-icon="el-icon-key"
                placeholder="请输入验证码"
              >
              </el-input>
              <div class="code" @click="changeCode">
                <img :src="codeUrl" alt="" />
              </div>
            </div>
          </el-form-item>
          <el-checkbox class="checkBox" v-model="change">
            <span
              >我已阅读并同意<a href="#">用户协议</a>和<a href="#"
                >隐私条款</a
              ></span
            >
          </el-checkbox>
          <el-form-item class="btn">
            <el-button class="loginBtn" @click="submit">登录</el-button>
            <el-button class="registerBtn" @click="registerBtn"
              >注册
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <loginOut ref="loginOut"></loginOut>
    <div>
      <img class="bgc" src="@/assets/img/login_banner_ele.png" alt="" />
    </div>
  </div>
</template>

<script>
import loginOut from './loginOut.vue'
import { captchaTypeLogin } from '@/api/login.js'
export default {
  components: {
    loginOut
  },
  data () {
    return {
      // type: 'login',
      // codeUrl: process.env.VUE_APP_BASE_API + '/captcha?type=login',
      codeUrl: '',
      form: {
        phone: '18511111111',
        password: '12345678',
        code: ''
      },
      change: false,
      rules: {
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号！'))
            }
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 16, message: '请输入6-16位验证码', trigger: 'change' }
        ],
        code: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  async created () {
    this.getCode()

    // console.log(process.env.VUE_APP_BASE_API + '/captcha?type=login')
    // console.log(this.codeUrl)
  },
  methods: {
    // 刷新验证码
    changeCode () {
      this.getCode()
    },
    // 生成验证码
    async getCode () {
      const res = await captchaTypeLogin()
      const blob = new Blob([res], { type: 'image/jpg' })
      const Fr = new FileReader()
      Fr.readAsDataURL(blob)
      Fr.onload = event => {
        this.codeUrl = event.target.result
      }
      console.log(res)
    },
    registerBtn () {
      this.$refs.loginOut.isShow = true
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          console.log(this.change)
          if (this.change === false) {
            this.$message.error('请确定您已经阅读用户协议！')
          } else {
            console.log(this.form)
            await this.$store.dispatch('user/getToken', this.form)
            // if (this.form.code) {
            //   this.$router.push('/layout/discipline')
            //   this.$message.success('登录成功!')
            // } else {
            //   this.$message.success('登录失败!')
            // }
          }
        }
      })
    }
  }
}
</script>

<style lang="less">
html,
body {
  background-color: #119bfa;
}
.login {
  .bgc {
    position: absolute;
    right: 100px;
    top: 200px;
  }
  .loginBox {
    margin-top: 119px;
    margin-left: 73px;
    width: 478px;
    height: 550px;
    background-color: #f5f5f5;
    padding-left: 43px;
    padding-right: 41px;
    padding-top: 43.75px;
    .title {
      display: flex;
      font-size: 0;
      .img {
        margin-right: 16px;
        margin-left: 48px;
        width: 22px;
        height: 17px;
        margin-top: 2px;
      }
      h3 {
        font-size: 22px;
        font-weight: 400;
        line-height: 22px;
      }
    }
    .form {
      margin-top: 26px;

      .one {
        margin-bottom: 25px;

        height: 45px;
      }
      .two {
        margin-bottom: 26px;
        height: 42px;
      }
      .three {
        margin-bottom: 28px;
        .input {
          display: flex;
          .codeText {
            flex: 1;
            display: inline-block;
          }
          .code {
            width: 110px;
            height: 42px;
            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
      .checkBox {
        margin-bottom: 32px;
        a {
          color: #1493fa;
        }
      }
      .btn {
        .loginBtn {
          display: block;
          width: 100%;
          margin-bottom: 26px;
          background-color: #1493fa;
          color: #fff;
        }
        .registerBtn {
          margin-left: 0;
          display: block;
          width: 100%;
          background-color: #1493fa;
          color: #fff;
        }
      }
    }
  }
}
</style>
